﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace WindowsFormsApplication1.DAL
{
    class DataAccess
    {
        SqlConnection con;

        public DataAccess()
        {
            string connString = "Data Source=JOHNWINDOWS7;Initial Catalog=SQLDemo; User ID=sa; password=aztaco";

            con = new SqlConnection(connString);


        }


        public DataTable GetPersonList()
        {
            DataTable results = new DataTable();


            //get the data from the database
            try
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("Select * from People", con);
                cmd.CommandType = CommandType.Text;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(results);

            }
            catch (Exception ex)
            {
                //error handling here!
                throw ex;
            }
            finally
            {
                con.Close();
            }

            return results;
        }

        public void AddPerson(string fName, string lName, string phone, string color)
        {
            //insert data into the database
            try
            {
                con.Open();

                string cmdText = String.Format(@"INSERT INTO [People]
                                               ([FName]
                                               ,[LName]
                                               ,[Phone]
                                               ,[BrushColor])
                                                VALUES
                                               ('{0}','{1}','{2}','{3}')", fName, lName, phone, color);
                SqlCommand cmd = new SqlCommand(cmdText, con);
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                //error handling here!
                throw ex;
            }
            finally
            {
                con.Close();
            }
        }




    }
}
